#!/bin/sh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2004 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
#
# (c) Copyright IBM Corporation 2000, 2003. All rights reserved.
#
# IBM Director Agent Install script
#
# Please only modify the User Configuration Section.
#

#########################################################
# Start of User Configuration Section

# SRCDIR = the location where the Director RPMs can be
#     found.  If the RPMs are in the same directory as
#     this script, leave SRCDIR as is.
#
SRCDIR=`pwd`

# LogFile = the location for a file that captures the
#     results of the installation; if LogFile line starts
#     with a #, no log will be created
#
LogFile=/var/log/dirinst.log

# DebugInstall = set to 1 to obtain very verbose logging
#     information from the installation
#
DebugInstall=0

# The following components are required.  However, if the
# current version is already installed, you may specify
# 0 to not install them.
#
DirectorAgent=1

# End of User Configuration Section
#########################################################

if [ "$DebugInstall" = "1" ]; then
   rpmOpts="-Uvvh"
else
   rpmOpts="-Uvh"
fi

#########################################################
smartUpgrade ( )
{
  # check for version independent RPM package
  rpm -q $1 > /dev/null 2>&1
  rc=$?
  # RPM returns 0 if package is found
  if [ "$rc" = "0" ]; then
     # check for same version RPM package
     rpm -q $2 > /dev/null 2>&1
     rc=$?
     if [ "$rc" = "0" ]; then
        # same version is already installed
        return 2
     else
        # different version is installed
        return 1
     fi
  fi
  return 0
}


upgradeRPM ( )
{
  echo "Attempting to install $1"
  rpm $rpmOpts $SRCDIR/$1
}


doInstalls ( )
{
  returncode="0"

  # Determine existing 4.x install directory (if any)
  if [ -f /etc/ibm/director/twgagent/twgagent ]; then
     . /etc/ibm/director/twgagent/twgagent
     relocatePath="$TWG_ROOTDIR"
  fi

  # Set rpm command line parameters
  if [ "$DebugInstall" = "1" ]; then
     rpmOpts="-Uvvh"
     rpmEOpts="-evv"
  else
     rpmOpts="-Uvh"
     rpmEOpts="-ev"
  fi
  if [ "$relocatePath" ]; then
     rpmOpts="$rpmOpts --relocate /opt/ibm/director=$relocatePath"
  fi

  # Director agent
  smartUpgrade ITDAgent ITDAgent-4.12-1
  upgradeAgent=$?
  if [ "$upgradeAgent" = "2" ]; then
     echo "ITDAgent-4.12-1.ppc.rpm is already installed."
  elif [ "$upgradeAgent" = "1" ] || \
       [ "$DirectorAgent" = "1" ]; then
     upgradeRPM ITDAgent-4.12-1.ppc.rpm
     returncode=$?
  fi


  #####################################################
  #            Done - give finish message             #
  #####################################################
  if [ "$returncode" = "0" ]; then
     echo ""
     echo "Installation of selected components is successful."
     echo "To start the IBM Director Agent manually, run /opt/ibm/director/bin/twgstart"
  else
     echo ""
     echo "Installation of selected components has failed."
     echo "See errors above for more details."
  fi

}


if [ "$LogFile" ]; then
   echo "#########################################################" >> $LogFile
   echo "Start of IBM Director Agent Install on" `date` >> $LogFile
fi

if [ "$LogFile" ]; then
   doInstalls 2>&1 | tee -a $LogFile
else
   doInstalls
fi

if [ "$LogFile" ]; then
   echo "End of IBM Director Agent Install on" `date` >> $LogFile
   echo "#########################################################" >> $LogFile
fi

